home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2646 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.1 KB  |  111 lines

  1. Path: news.ssnet.com!not-for-mail
  2. From: helie@ssnet.com (Ray Helie)
  3. Newsgroups: comp.lang.c++
  4. Subject: Virtual Members: Difficult Question
  5. Date: 18 Jan 1996 17:24:06 -0500
  6. Organization: SSNet, Inc.
  7. Message-ID: <4dmha6$80c@marlin.ssnet.com>
  8. NNTP-Posting-Host: marlin.ssnet.com
  9.  
  10. I have a difficult problem that no one can seem to solve for me.  If
  11. anyone can shed some light, please do:
  12.  
  13. In the microsoft foundation class library, there is a CObject class.  I
  14. create an inherited class that is a virtual class as follows:
  15.  
  16. class C_BASE : public CObject
  17.     {
  18.     // ...
  19.     virtual void index () = 0;
  20.     };
  21.  
  22.  
  23. I don't really use any of the functions out of CObject, but I have to
  24. inherit it to pass C_BASE to other objects.  Anyway, I then define
  25. a further inherited class that actually defines the virtual function:
  26.  
  27. class C_ITEM : public C_BASE
  28.     {
  29.     // ...
  30.     void index () { ... };
  31.     };
  32.  
  33.  
  34. Now here's the question: normally, you can have a base class (with
  35. a virtual function), and then several derived classes, and using
  36. a base class pointer, access the virtual function defined in the
  37. base class, where it will know which derived class' function to 
  38. actually call.  Well, I took it possibly a step too far, and I
  39. need to know if this is ok to do: (and this part is a prelude to the tough
  40. part):
  41.  
  42. void main ()
  43.     {
  44.     C_ITEM item;
  45.     func1 ((CObject*)&item);
  46.     }
  47.  
  48. void func1 (CObject* pItem)
  49.     {
  50.     C_BASE* base;
  51.     base = pItem;
  52.     pItem-> index (); 
  53.     // (i) is the above call to index () allowed?  or did i lose virtual 
  54.     // information about the class C_BASE when I went to the CObject
  55.     // pointer?
  56.     }
  57.  
  58.  
  59.  
  60. That seems to work for me, but I thought I'd make sure it's something
  61. that is truly legal that is supposed to work *all* the time.  
  62.  
  63. On to the difficult part:
  64.  
  65. I'm reading and writing object states to disk.  I've have need to do it
  66. the following way, so what I need to know is why the following way
  67. doesn't seem to work and how I can get around it:
  68.  
  69.  
  70. main ()
  71.     {
  72.     C_ITEM item;
  73.     writeObject ((CObject*)&item,sizeof(C_ITEM));
  74.     readObject  ((CObject*)&item,sizeof(C_ITEM));
  75.     }
  76.  
  77.  
  78. void readObject (CObject* pObject,int pSize)
  79.     {
  80.     C_BASE* base = pObject;
  81.     base-> index (); // this call works fine
  82.     Read ((char*)&pObject,pSize); 
  83.     // this reads in the character string from beginning of a file
  84.  
  85.     base-> index (); // !!! this call now crashes the program !!!
  86.  
  87.     }
  88.  
  89.  
  90. void writeObject (CObject* pObject,int pSize)
  91.     {
  92.     Write ((char*)pObject,pSize);
  93.     // this will write a string of characters to the start of a file
  94.     // ...
  95.     }
  96.  
  97.  
  98. So why does the line in readObject () crash-- it's a virtual function
  99. call that worked before I read in the data overtop the object.  How
  100. is the knowledge of where the virtual function's address is getting 
  101. corrupted by my writing over the object's memory location?  This
  102. problem seems to require some sort of detailed knowledge of how
  103. things are truly implemented, so if anyone can shed any insight
  104. into this problem, I would greatly appreciate it-- no one seems to
  105. be able to figure it out around here.  :)  Feel free to email me,
  106. because I don't know how interesting the answer would be for
  107. everyone else.  Thanks in advance.
  108.  
  109.  
  110. Ray Helie [helie@ssnet.com]
  111.